package com.windystory.wslibrary.base.fragment;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.windystory.wslibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 刷新
 */
public abstract class BaseRefreshFragment<T> extends BaseFragment {

    public SmartRefreshLayout refresh;
    public RecyclerView recycleView;
    public BaseQuickAdapter adapter;
    public int page = 1;
    public LinearLayout linear_loading;
    public LinearLayout linear_error;
    public List<T> dataList = new ArrayList<>();


    @Override
    public int getLayoutResId() {
        return R.layout.activity_base_refresh;
    }

    @Override
    protected void initView(View v) {
        refresh = v.findViewById(R.id.refreshLayout);
        recycleView = v.findViewById(R.id.recyclerView);
        linear_loading = v.findViewById(R.id.linear_skeleton);
        linear_error = v.findViewById(R.id.linear_error);
    }

    @Override
    protected void initData() {
        dataList = setList();
        adapter = setAdapter(dataList);
        refresh.setEnableAutoLoadMore(true);
        recycleView.setLayoutManager(setLayoutManager());
        recycleView.setAdapter(adapter);
        adapter.bindToRecyclerView(recycleView);

        initEmptyView(recycleView, adapter);

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData(page);

            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData(page);
            }
        });

        getData(page);

    }


    protected abstract List<T> setList();

    /**
     * 设置空状态layout
     *
     * @param recycleView
     * @param adapter
     */
    private void initEmptyView(RecyclerView recycleView, BaseQuickAdapter adapter) {
        if (setEmptyView() != 0) {
            adapter.setEmptyView(setEmptyView(), recycleView);
        }
    }

    /**
     * 设置空状态layout
     */
    protected abstract int setEmptyView();


    @Override
    protected void initListener() {
    }


    /**
     * 设置LayoutManager
     *
     * @return
     */
    protected abstract RecyclerView.LayoutManager setLayoutManager();

    /**
     * 设置适配器
     *
     * @param ts
     * @return
     */
    protected abstract BaseQuickAdapter setAdapter(List<T> ts);

    /**
     * 获取数据
     *
     * @param page
     */
    protected abstract void getData(int page);

    public void showLoadingView() {
        linear_loading.setVisibility(View.VISIBLE);
    }

    public void dissmissLoadingView() {
        linear_loading.setVisibility(View.GONE);
    }

    /**
     * 显示网络错误
     */
    private void showErrorLayout() {
        linear_error.setVisibility(View.VISIBLE);
    }

    /**
     * 不显示网络错误
     */
    private void dissmissErrorLayout() {
        linear_error.setVisibility(View.GONE);
    }

    @Override
    public void withOutNetWork() {
        showErrorLayout();
    }

    @Override
    public void withMobileNetWork() {
        dissmissErrorLayout();
    }

    @Override
    public void withWiFiNetWork() {
        dissmissErrorLayout();
    }

    @Override
    public void withMobile2WiFiNetWork() {
        dissmissErrorLayout();
    }

}
